<?php

/**
 * Created by allen.qiang <allen.qiang@fistforward.com>
 * Date: 15-1-8
 * Time: 下午4:24
 * FUN-AC-0028
 * 下午茶套餐
 */
quanli_require('module/base/system/itemHandler.class.php');//载入字典操作类
quanli_require('module/base/dunch/dunch.class.php');//载入下午茶操作类

class ajax_dunch {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && quanli_is_int($_REQUEST['operFlg'])) {

            switch ($_REQUEST['operFlg']) {

                case 1: //新增套餐
                    $this->addPackage();
                    break;
                case 2:  //套餐配置列表
                    $this->dunchPackage();
                    break;
                case 3: //查看套餐信息
                    $this->selPackage();
                    break;
                case 4:// 修改套餐信息
                    $this->updPackage();
                    break;
                case 5://删除套餐信息
                    $this->delPackage();
                    break;
                case 6://添加下午茶选项
                    $this->addx();
                    break;
                case 7://下午茶列表
                    $this->dunchList();
                    break;
                case 8://查看下午茶信息
                    $this->selDunch();
                    break;
                case 9://修改下午茶信息
                    $this->updDunch();
                    break;
                case 10://删除下午茶信息
                    $this->delDunch();
                    break;


                case 100://添加咖啡验收信息
                    $this->addCoffeeInfo();
                    break;
                case 101://咖啡验收信息列表
                    $this->selCoffee();
                    break;
                case 102://咖啡验收信息明细
                    $this->coffeeInfo();
                    break;


                case 201: //同步会员与地址
                    $this->importData();
                    break;

                default:
                    returnJsonData('参数错误');
                    break;

            }

        } else {

            returnJsonData('参数错误');

        }

    }

    /**
     * 新增套餐配置信息
     */
    private function addPackage() {

        global $G_PGLOB;

        $packageName = $_REQUEST['packageName'];//套餐名称
        $cakeName = $_REQUEST['cakeName'];//蛋糕编号
        $coffeeName = $_REQUEST['coffeeName'];//咖啡编号
        $itemName = $_REQUEST['itemName'];//蛋糕图片路径
        $itemName_2 = $_REQUEST['itemName_2'];//小图片路径
        $itemName_3 = $_REQUEST['itemName_3'];//咖啡图片路径
        $package_price = $_REQUEST['package_price'];//价格
        $package_remark = $_REQUEST['package_remark'];//描述
        $package_remark_2 = $_REQUEST['package_remark_2'];//描述
        //插入套餐主表信息
        $dunch_package=new entity("dunch_package");
        $dunch_package->creator=$G_PGLOB->currentUserName;
        $dunch_package->package_name=$packageName;
        $dunch_package->package_price=$package_price;
        $dunch_package->package_image=$itemName;
        $dunch_package->package_image_2=$itemName_2;
        $dunch_package->package_image_3=$itemName_3;
        $dunch_package->package_remark=$package_remark;
        $dunch_package->package_remark_2=$package_remark_2;
        $G_PGLOB->DBCA->save($dunch_package);
        $selSql="select package_id from dunch_package where uuid=?";
        $quest=$G_PGLOB->DBCA->stmt_query($selSql,'s',$dunch_package->uuid);
        //插入套餐子表信息(蛋糕)
        $dunch_package_detail=new entity("dunch_package_detail");
        $dunch_package_detail->creator=$G_PGLOB->currentUserName;
        $dunch_package_detail->package_id=$quest[0]->package_id;
        $dunch_package_detail->product_code=$cakeName;
        $G_PGLOB->DBCA->save($dunch_package_detail);
        //插入套餐子表信息(咖啡)
        $dunch_package_detail=new entity("dunch_package_detail");
        $dunch_package_detail->creator=$G_PGLOB->currentUserName;
        $dunch_package_detail->package_id=$quest[0]->package_id;
        $dunch_package_detail->product_code=$coffeeName;
        $G_PGLOB->DBCA->save($dunch_package_detail);

        returnJsonData('新增成功', 200);
    }

    /*
     * 查询下午茶套餐列表
     */
    public function dunchPackage(){

        global $G_PGLOB;

//
//        $packageListSql="select  * from dunch_package";
//        $pcakageListArray=$G_PGLOB->DBCA->stmt_query($packageListSql);
        //调用查询字典信息方法
        $dunch=new dunch();
        $pcakageListArray = $dunch->searchPackage( $_REQUEST );

        //组织返回的数据
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ( $pcakageListArray ) {
            foreach ( $pcakageListArray as $pcakageList ) {
                //编辑字符串
                $editString = '';
                $editString .='<a onclick="basicSetting.edit( ' . $pcakageList['package_id'] . ' )" href="javascript: void(0);">修改</a>&nbsp;&nbsp;&nbsp;';
                $editString .='<a onclick="basicSetting.delPackge( ' . $pcakageList['package_id'] . ' )" href="javascript: void(0);">删除</a>&nbsp;&nbsp;&nbsp;';
                $result_array['rows'][] = array(
                    'id' => $pcakageList['package_id'],
                    'cell' => array(
                        $editString,
                        $pcakageList['package_id'],
                        $pcakageList['package_name'],
                        $pcakageList['package_price'],
                        $pcakageList['package_remark'],
                        $pcakageList['package_remark_2']
                    )
                );
            }
        }
        echo json_encode( $result_array );

    }

    /*
     * 查看套餐信息
     */
    public function selPackage(){

        global $G_PGLOB;

        $package_id=$_REQUEST['package_id'];
        $dunch_packageSql="select  * from dunch_package where package_id=?";
        $dunch_packageArray=$G_PGLOB->DBCA->stmt_query($dunch_packageSql,'i',$package_id);


        $dunch_package_detailSql="select  * from dunch_package_detail where package_id=?";
        $dunch_package_detailArray=$G_PGLOB->DBCA->stmt_query($dunch_package_detailSql,'i',$package_id);

        $itemHandler=new itemHandler();
        $arg_array=array(
            'item'=>36
        );
        $dunchinfo=$itemHandler->searchItemList($arg_array);
        $coffee=str_replace("'",'',$dunchinfo[0]->itemName);
        $coffeeArray=explode(',',$coffee);
        if(in_array($dunch_package_detailArray[1]->product_code,$coffeeArray)){
            $coffeeCode=$dunch_package_detailArray[1]->product_code;
            $cakeCode=$dunch_package_detailArray[0]->product_code;
        }else{
            $coffeeCode=$dunch_package_detailArray[0]->product_code;
            $cakeCode=$dunch_package_detailArray[1]->product_code;
        }


        $returnArray=array(
            'package_id'=>$dunch_packageArray[0]->package_id,
            'package_name'=>$dunch_packageArray[0]->package_name,
            'coffee'=>$coffeeCode,
            'cake'=>$cakeCode,
            'package_price'=>$dunch_packageArray[0]->package_price,
            'package_image'=>$dunch_packageArray[0]->package_image,
            'package_image_2'=>$dunch_packageArray[0]->package_image_2,
            'package_image_3'=>$dunch_packageArray[0]->package_image_3,
            'package_remark'=>$dunch_packageArray[0]->package_remark,
            'package_remark_2'=>$dunch_packageArray[0]->package_remark_2
        );

        returnJsonData('ok', 200, $returnArray);

    }

    /*
     * 修改套餐信息
     */
    public function updPackage(){
        global $G_PGLOB;

        $package_id=$_REQUEST['package_id'];
        $packageName = $_REQUEST['packageName'];//套餐名称
        $cakeName = $_REQUEST['cakeName'];//蛋糕编号
        $coffeeName = $_REQUEST['coffeeName'];//咖啡编号
        $itemName = $_REQUEST['itemName'];//蛋糕图片路径
        $itemName_2 = $_REQUEST['itemName_2'];//图片路径
        $itemName_3 = $_REQUEST['itemName_3'];//咖啡图片路径
        $package_price = $_REQUEST['package_price'];//价格
        $package_remark = $_REQUEST['package_remark'];//描述
        $package_remark_2 = $_REQUEST['package_remark_2'];//描述
        $dunch_package=new entity("dunch_package");
        $dunch_package->package_id=$package_id;
        $G_PGLOB->DBCA->fetch($dunch_package);
        $dunch_package->package_name=$packageName;
        $dunch_package->package_price=$package_price;
        $dunch_package->package_image=$itemName;
        $dunch_package->package_image_2=$itemName_2;
        $dunch_package->package_image_3=$itemName_3;
        $dunch_package->package_remark=$package_remark;
        $dunch_package->package_remark_2=$package_remark_2;
        $G_PGLOB->DBCA->save($dunch_package);

        //删除数据集
        $delSql="DELETE FROM dunch_package_detail WHERE package_id= ?";
        $G_PGLOB->DBCA->stmt_query($delSql,'i',$package_id);


        //插入套餐子表信息(蛋糕)
        $dunch_package_detail=new entity("dunch_package_detail");
        $dunch_package_detail->creator=$G_PGLOB->currentUserName;
        $dunch_package_detail->package_id=$package_id;
        $dunch_package_detail->product_code=$cakeName;
        $G_PGLOB->DBCA->save($dunch_package_detail);
        //插入套餐子表信息(咖啡)
        $dunch_package_detail=new entity("dunch_package_detail");
        $dunch_package_detail->creator=$G_PGLOB->currentUserName;
        $dunch_package_detail->package_id=$package_id;
        $dunch_package_detail->product_code=$coffeeName;
        $G_PGLOB->DBCA->save($dunch_package_detail);


        returnJsonData('修改成功', 200);
    }
    /*
     * 删除套餐信息
     */
    public function delPackage(){
        global $G_PGLOB;
        //接收参数
        $package_id = $_REQUEST['package_id'];

        //统计不存在的选项分类数目
        $errorNum = 0;
        //检查要删除的选项分类
        $dunch_package = new entity('dunch_package');
        $dunch_package->package_id = $package_id;

        if (!$G_PGLOB->DBCA->fetch($dunch_package)) {
            $errorNum++;  //记录不存在的选项分类数
        }
        if ($errorNum > 0) {
            returnJsonData('所选部分信息已不存在，请刷新页面', 400);
        }
        $G_PGLOB->DBCA->delete($dunch_package);


        //删除套餐明细表
        $delSql="delete from dunch_package_detail where  package_id=?";

        $G_PGLOB->DBCA->stmt_query($delSql,'s',$package_id);

        returnJsonData('删除成功', 200);
    }










    /**
     * 查询基础设置分类详情（单个）
     * @global type $G_PGLOB
     * @return type
     */
    private function searchItemCategoryDetail() {

        global $G_PGLOB;

        if (!isset($_REQUEST['itemCategoryID']) && quanli_is_int($_REQUEST['itemCategoryID'])) {
            returnJsonData('参数错误');
        }

        //实例基础设置类
        $itemHandler = new itemHandler();

        $args_array = array(
            'itemCategoryID' => $_REQUEST['itemCategoryID']
        );
        $item_category_array = $itemHandler->searchCategory($args_array);
        if ($item_category_array) {
            returnJsonData('ok', 200, $item_category_array[0]);
        } else {
            returnJsonData('分类信息没有找到', 404);
        }
    }

    /**
     *  查询分类下的选项信息
     */
    private function searchItem() {

        global $G_PGLOB;

        if (!isset($_REQUEST['itemCategoryID']) && quanli_is_int($_REQUEST['itemCategoryID'])) {
            returnJsonData('参数错误');
        }

        //实例基础设置类
        $itemHandler = new itemHandler();

        //查询选项
        $args_array = array(
            'itemCategoryID' => $_REQUEST['itemCategoryID']
        );

        //判断是否为切分（编号为8）从小到大排序
        if( $_REQUEST['itemCategoryID'] ==8 ){
            $args_array = array(
                'itemCategoryID' => $_REQUEST['itemCategoryID'],
                'extend'=>' ORDER BY CAST(itemName AS SIGNED) ASC '
            );
        }
        $item_array = $itemHandler->searchItem($args_array);

        returnJsonData('ok', 200, $item_array);
    }

    /**
     * 删除选项分类信息
     */
    private function delItem() {
        global $G_PGLOB;
        //接收参数
        $itemID = $_REQUEST['itemID'];

        //统计不存在的选项分类数目
        $errorNum = 0;
        //检查要删除的选项分类
        $sys_item = new entity('sys_item');
        $sys_item->itemID = $itemID;

        if (!$G_PGLOB->DBCA->fetch($sys_item)) {
            $errorNum++;  //记录不存在的选项分类数
        }
        if ($errorNum > 0) {
            returnJsonData('所选部分信息已不存在，请刷新页面', 400);
        }
        $G_PGLOB->DBCA->delete($sys_item);
        returnJsonData('删除成功', 200);
    }

    /**
     * 编辑分类名称
     */
    private function itemCategoryEdit() {

        global $G_PGLOB;

        //接收参数
        $itemCategoryID = $_REQUEST['itemCategoryID'];
        $itemCategoryName = $_REQUEST['itemCategoryName'];
        //检验参数格式
        if (empty($itemCategoryName)) {

            returnJsonData('分类名称不能为空', 400);
        }

        $sys_itemcategory = new entity('sys_itemcategory');

        if (!empty($itemCategoryID)) {

            $sys_itemcategory->itemCategoryID = $itemCategoryID;

            if (!$G_PGLOB->DBCA->fetch($sys_itemcategory)) {

                returnJsonData('没有找到当前信息', 404);
            }
            //修改时检查是否重复
            $sql = "SELECT itemCategoryID FROM sys_itemcategory WHERE itemCategoryID != ? AND itemCategoryName = ?";
            $item_temp_array = $G_PGLOB->DBCA->stmt_query( $sql, 'is', array($itemCategoryID, $itemCategoryName) );

            if ( $item_temp_array ) {
                returnJsonData('分类名称已存在', 400);
            }
        } else {
            $sys_itemcategory->itemCategoryName = $itemCategoryName;

            //添加时是否重复
            if( $G_PGLOB->DBCA->fetch($sys_itemcategory) ){

                returnJsonData('分类名称已存在', 400);

            }
            $sys_itemcategory->creator = $G_PGLOB->currentUserID;
        }
        $sys_itemcategory->editor = $G_PGLOB->currentUserID;
        $sys_itemcategory->itemCategoryName = $itemCategoryName;
        $G_PGLOB->DBCA->save($sys_itemcategory);
        if ($itemCategoryID) {

            returnJsonData('更新成功', 200);

        } else {
            returnJsonData('保存成功', 200);
        }
    }

    /**
     * 启用或禁用选项分类
     */
    private function changeItemStatus() {

        global $G_PGLOB;

        $useFlg = $_REQUEST['useFlg'];
        if (isset($_REQUEST['itemID']) && quanli_is_int($_REQUEST['itemID'])) {

            $itemID = $_REQUEST['itemID'];
            $sys_item = new entity('sys_item');
            $sys_item->itemID = $itemID;
            $G_PGLOB->DBCA->fetch($sys_item);

            if ($useFlg == 1 && $sys_item->useFlg == 1) {//状态为1 启用 状态为0 禁用
                $sys_item->useFlg = 0;
                //add by allen.qiang 20141219 保存修改人信息 start
                $sys_item->editor = $G_PGLOB->currentUserName;
                //add by allen.qiang 20141219 保存修改人信息 end
                $G_PGLOB->DBCA->save($sys_item);
                returnJsonData('禁用成功', 200);
            } else if (($useFlg == 1 && $sys_item->useFlg == 0) || ($useFlg == 0 && $sys_item->useFlg == 1)) {

                returnJsonData('当前数据信息已改变，请刷新页面', 400);

            } else {
                $sys_item->useFlg = 1;
                //add by allen.qiang 20141219 保存修改人信息 start
                $sys_item->editor = $G_PGLOB->currentUserName;
                //add by allen.qiang 20141219 保存修改人信息 end
                $G_PGLOB->DBCA->save($sys_item);

                returnJsonData('启用成功', 200);
            }
        } else {
            returnJsonData('您输入的信息有误', 400);
        }
    }

    /**
     * 查询基础设置分类选项详情
     * @global type $G_PGLOB
     */
    private function searchItemDetail(){
        global $G_PGLOB;

        if (!isset($_REQUEST['itemID']) && quanli_is_int($_REQUEST['itemID'])) {
            returnJsonData('参数错误');
        }
        //实例基础设置类
        $itemHandler = new itemHandler();

        //查询选项
        $args_array = array(
            'itemID' => $_REQUEST['itemID']
        );
        $item_array = $itemHandler->searchItem($args_array);
        if ($item_array) {
            returnJsonData('ok', 200, $item_array[0]);
        } else {
            returnJsonData('选项信息没有找到',  404);
        }
    }

    /**
     * 添加或编辑选项分类
     */
    private function itemEdit() {

        global $G_PGLOB;

        $_REQUEST['operator'] = $G_PGLOB->currentUserID;

        //根据itemID 判断 存在itemID更新 不存在添加
        if (!isset($_REQUEST['itemID']) && quanli_is_int($_REQUEST['itemID'])) {
            returnJsonData('参数错误');
        }
        //分类号
        if ( !isset( $_REQUEST['itemCategoryID'] ) && quanli_is_int($_REQUEST['itemCategoryID'] ) ) {
            returnJsonData('参数错误');
        }

        $itemName = trim( isset($_REQUEST['itemName'] ) ? $_REQUEST['itemName'] : "" );                //名称

        if ( $itemName === '' ) {
            returnJsonData('选项分类名称不能为空', 400);
        }
        $sys_item = new entity( 'sys_item' );

        if ( !empty( $_REQUEST['itemID'] ) ) {

            $sys_item->itemID = $_REQUEST['itemID'];

            if ( !$G_PGLOB->DBCA->fetch( $sys_item ) ) {
                returnJsonData('没有找到当前信息', 400);
            }
            if( $_REQUEST['itemCategoryID'] == 1){

                if( $sys_item->extend_1 ){
                    $extend_1_db = $sys_item->extend_1;
                }else{
                    $extend_1_db = '';
                }
            }

        } else {

            $sys_item->creator = $G_PGLOB->currentUserID;
            $sys_item->useFlg = 1;
        }

        //add by 2014-12-03 促销渠道加部门信息 xuhongshun  start

        if( $_REQUEST['itemCategoryID'] == 1){
            $sys_item->extend_1 = $_REQUEST['search_departmentID'];
            $sys_item->extend_2 = $_REQUEST['departmentName'];
        }
        //add by 2014-12-03 促销渠道加部门信息 xuhongshun  end

        $sys_item->itemName = $itemName;                                               //选项分类名称
        $sys_item->itemCategoryID = $_REQUEST['itemCategoryID'];                              //分类号
        //add by allen.qiang 20141219 保存修改人信息 start
        $sys_item->editor = $G_PGLOB->currentUserName;
        //add by allen.qiang 20141219 保存修改人信息 end
        $G_PGLOB->DBCA->save( $sys_item );

        //mod by 2014-12-04 促销渠道加部门信息 xuhongshun  start
        if( $_REQUEST['itemCategoryID'] == 1){
            $returnArr = array(
                'itemID'=>$G_PGLOB->DBCA->insert_id,
                'itemName'=>$itemName,
                'extend_1'=>$_REQUEST['search_departmentID'],
                'extend_1_db'=>$extend_1_db,
                'extend_2'=>$_REQUEST['departmentName'],
                'itemCategoryID'=>$_REQUEST['itemCategoryID']
            );
        }else{
            $returnArr = array(
                'itemID'=>$G_PGLOB->DBCA->insert_id,
                'itemName'=>$itemName,
                'itemCategoryID'=>$_REQUEST['itemCategoryID']
            );
        }
        //mod by 2014-12-04 促销渠道加部门信息 xuhongshun  end

        if ($_REQUEST['itemID']) {
            returnJsonData('更新成功', 200,$returnArr);
        } else {
            returnJsonData('保存成功', 200,$returnArr);
        }
    }

    /*
     * 添加下午茶信息
     */
    public function addx(){

        global $G_PGLOB;

        $sale_date = $_REQUEST['sale_date'];//配送日期
        $package_id_array = $_REQUEST['package_id'];//蛋糕编号
        $num_array = $_REQUEST['num'];//数量
        $right_num = $_REQUEST['right_num'];//权益数量

        foreach($package_id_array as $key=>$package_id ){

            $dunch_sale=new entity("dunch_sale");
            $dunch_sale->creator=$G_PGLOB->currentUserName;
            $dunch_sale->sale_date=$sale_date;
            $dunch_sale->package_id=$package_id;
            $dunch_sale->package_num=$num_array[$key];
            $dunch_sale->right_num=$right_num;
            $dunch_sale->use_num=0;

            $G_PGLOB->DBCA->save($dunch_sale);

        }
        returnJsonData('保存成功', 200);
    }
    /*
     * 获取下午茶列表信息
     */
    public function dunchList(){
        global $G_PGLOB;
//        $packageListSql="select  * from dunch_package";
//        $pcakageListArray=$G_PGLOB->DBCA->stmt_query($packageListSql);
        //调用查询字典信息方法
        $dunch=new dunch();
        $duncheListArray = $dunch->searchDunch( $_REQUEST );
        //组织返回的数据
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );
        if ( $duncheListArray ) {
            foreach ( $duncheListArray as $dunchList ) {

                //编辑字符串
                $editString = '';
                $editString .="<a onclick=basicSetting.edit('".$dunchList['sale_date']."') href='javascript: void(0);'>修改</a>&nbsp;&nbsp;&nbsp;";
                $editString .="<a onclick=basicSetting.delPackge('".$dunchList['sale_id']."') href='javascript: void(0);'>删除</a>&nbsp;&nbsp;&nbsp;";
                $result_array['rows'][] = array(
                    'id' => $dunchList['sale_id'],
                    'cell' => array(
                        $editString,
                        $dunchList['sale_id'],
                        $dunchList['sale_date'],
                        $dunchList['package_name'],
                        $dunchList['package_num'],
                        $dunchList['right_num'],
                        $dunchList['use_num']
                    )
                );
            }
        }
        echo json_encode( $result_array );
    }
    /*
     * 查看下午茶详细信息
     */
    public function selDunch(){
        global $G_PGLOB;
        $sale_date=$_REQUEST['sale_date'];
        $dunchSql="select * from  dunch_sale where sale_date=?";
        $dunchQuery=$G_PGLOB->DBCA->stmt_query($dunchSql,'s',$sale_date);

        foreach($dunchQuery as $dunchArray){
            $tmpArray[]=array(
                'sale_date'=>$dunchArray->sale_date,
                'package_id'=>$dunchArray->package_id,
                'package_num'=>$dunchArray->package_num,
                'right_num'=>$dunchArray->right_num
            );

        }
        returnJsonData('ok', 200, $tmpArray);

    }
    /*
     * 修改下午茶信息
     */
    public function updDunch(){
        global $G_PGLOB;
        $sale_date = $_REQUEST['old_sale_date'];//配送日期
        $package_id_array = $_REQUEST['package_id'];//蛋糕编号
        $package_num_array = $_REQUEST['num'];//数量
        $right_num = $_REQUEST['right_num'];//数量
        $delSql="delete from dunch_sale where sale_date=?";
        $G_PGLOB->DBCA->stmt_query($delSql,'s',$sale_date);
        foreach($package_id_array as $key=>$package_id ){

            $dunch_sale=new entity("dunch_sale");
            $dunch_sale->creator=$G_PGLOB->currentUserName;
            $dunch_sale->sale_date=$sale_date;
            $dunch_sale->package_id=$package_id;
            $dunch_sale->package_num=$package_num_array[$key];
            $dunch_sale->right_num=$right_num;
            $dunch_sale->use_num=0;

            $G_PGLOB->DBCA->save($dunch_sale);

        }   returnJsonData('修改成功', 200);
    }
    /*
     * 删除下午茶信息
     */
    public function delDunch(){
        global $G_PGLOB;
        $sale_id=$_REQUEST['sale_id'];
        $delSql="delete from dunch_sale where sale_id=?";
        $G_PGLOB->DBCA->stmt_query($delSql,'s',$sale_id);
        returnJsonData('删除成功', 200);
    }



    /*
     * 添加咖啡验收信息
     */
    public function addCoffeeInfo(){

        global $G_PGLOB;
        $distribution_id = $_REQUEST['distribution_id'];//配送站
        $accept_date = $_REQUEST['accept_date'];//验收日期
        $remark = $_REQUEST['remark'];//备注
        $coffeesku = $_REQUEST['coffeesku'];//咖啡SKU
        $coffeenum = $_REQUEST['coffeenum'];//咖啡数量
        foreach($coffeesku as $key=>$coffeesku1){
            $newArray[]=array(
                'coffee'=>$coffeesku[$key],
                'num'=>$coffeenum[$key]
            );

        }
        $dunch_accept=new entity("dunch_accept");
        $dunch_accept->creator=$G_PGLOB->currentUserName;;
        $dunch_accept->brandID=1;
        $dunch_accept->distribution_id=$distribution_id;
        $dunch_accept->accept_date=$accept_date;
        $dunch_accept->remark=$remark;
        $G_PGLOB->DBCA->save($dunch_accept);
        $selSql="select accept_id from dunch_accept where uuid=?";
        $quest=$G_PGLOB->DBCA->stmt_query($selSql,'s',$dunch_accept->uuid);

        foreach($newArray as $newArray1){
            $dunch_accept_detail=new entity("dunch_accept_detail");
            $dunch_accept_detail->accept_id=$quest[0]->accept_id;
            $dunch_accept_detail->creator=$G_PGLOB->currentUserName;
            $dunch_accept_detail->product_code=$quest[0]->accept_id;
            $dunch_accept_detail->product_code=$newArray1['coffee'];
            $dunch_accept_detail->accept_amount=$newArray1['num'];
            $G_PGLOB->DBCA->save($dunch_accept_detail);
        }
        returnJsonData('新增成功', 200);

    }
    /*
     * 咖啡验收信息列表
     */
    public function selCoffee(){
        global $G_PGLOB;

        //调用查询字典信息方法
        $dunch=new dunch();
        $coffeeListArray = $dunch->searchCoffee( $_REQUEST );
        //组织返回的数据
        $result_array = array(
            'page' => $_REQUEST['page'],
            'total' => $_REQUEST['count'],
            'rows' => array()
        );

        if ( $coffeeListArray ) {


            foreach ( $coffeeListArray as $coffeeList ) {

                $detailSql="select dunch_accept_detail.*,goodsName from  dunch_accept_detail join goods_sale on dunch_accept_detail.product_code=goods_sale.product_code   where accept_id=?";
                $detailQuery=$G_PGLOB->DBCA->stmt_query($detailSql,'s',$coffeeList['accept_id']);
                $detailHtml='';
                foreach($detailQuery as  $detailArray){
                    $detailHtml.=$detailArray->goodsName;
                    $detailHtml.='*'.$detailArray->accept_amount.'<br/>';
                }

                //编辑字符串
                $editString = '';
               // $editString .="<a onclick=coffeeSetting.edit('".$coffeeList['accept_id']."') href='javascript: void(0);'>明细</a>&nbsp;&nbsp;&nbsp;";
                $result_array['rows'][] = array(
                    'id' => $coffeeList['accept_id'],
                    'cell' => array(
                       // $editString,
                      //  $coffeeList['accept_id'],
                        $coffeeList['creator'],
                        $coffeeList['createTime'],
                        $coffeeList['brandID']==1? "下午茶" :'',
                        $coffeeList['distribution_id']==48 ? "静安站":'',
                        $detailHtml,
                        $coffeeList['remark']
                    )
                );
            }
        }
        echo json_encode( $result_array );
    }

    /*
     * 咖啡验收明细信息
     */
    public function coffeeInfo(){
        global $G_PGLOB;
        $accept_id=$_REQUEST['accept_id'];
        $detailSql="select dunch_accept_detail.*,goodsName from  dunch_accept_detail join goods_sale on dunch_accept_detail.product_code=goods_sale.product_code   where accept_id=?";
        $detailQuery=$G_PGLOB->DBCA->stmt_query($detailSql,'s',$accept_id);
        $coffeeHtml='<table border="1">';
        foreach($detailQuery as $detailArray){

            $coffeeHtml.="<tr><td style='width:100px;'>咖啡名称：</td><td style='width:100px;'>".$detailArray->goodsName."</td><td style='width:100px;'>数量：</td><td style='width:100px;'>".$detailArray->accept_amount."</td></tr>";
        }
        $coffeeHtml.="</table>";
        returnJsonData('ok', 200, $coffeeHtml);
    }

    /*
     * 导入会员与地址信息
     * */
    public function importData()
    {
        global $G_PGLOB;
        quanli_require('module/base/customer/custDeliver.class.php');
        $custDeliver = new custDeliver();
        $address_array = $G_PGLOB->DBCA->stmt_query("SELECT * FROM dunch_address WHERE pcustID IS NOT NULL AND (deliverID IS NULL OR deliverID='' OR deliverID = 0)");

        if($address_array)
        {
            foreach($address_array as $address)
            {


                try
                {

                    if($address->address)
                    {
                        if( !$address->custName)
                        {
                            $address->custName = $address->custID;
                        }

                        $args_array = array(
                            'pcustID' => $address->pcustID ,
                            'creator' => $address->custID,
                            'provinceid' => $address->provinceid,
                            'cityid' => $address->cityid,
                            'countyid' => $address->countyid,
                            'address' => $address->address

                        );







                        $deliver_array_s = $custDeliver->searchCustDeliver( $args_array );


                        if($deliver_array_s)
                        {
                            $deliver_array = $deliver_array_s[0];
                        }
                        else
                        {
                            $args_array['mobile'] =  $address->custID;
                            $args_array['custName'] = $address->custName;
                            $result_deliver = $custDeliver->editCustDeliver( $args_array );

                            if ($result_deliver['code'] != 200) {
                                return $result_deliver;
                            }
                            else
                            {
                                $deliver_array = $result_deliver['data'];
                            }
                        }


                        $dunch_address = new entity('dunch_address');
                        $dunch_address->uuid = $address->uuid;

                        $G_PGLOB->DBCA->fetch($dunch_address);
                        if($dunch_address)
                        {
                            $dunch_address->deliverID = $deliver_array['deliverID'];
                            $G_PGLOB->DBCA->save($dunch_address);
                        }

                    }

                }catch (Exception $e)
                {
                    error_log(print_r($e->getMessage(),1));
                    //returnJsonData('error', 400, $e->getMessage());
                }


            }
        }
        returnJsonData('ok', 200, "成功");
    }

}

